/**
 * Created by oneminute on 7/24/14.
 */
var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var userFilter = require("../../routes/userfilter");

describe("测试UserFilter模块。", function () {
    var tmpUsers = [];

    before(function(done) {
        var user = new lz.User({id: lz.Utils.getUUID("u_")});
        user.save(function() {
            tmpUsers.push(user);
            done();
        });
    });

    it("测试没有uid传入的情况。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search'
        });
        var response = httpMocks.createExpressResponse();
        var next = function (_req, _res) {
            testutils.check(done, function () {
                assert.notEqual(_req.query.uid, null);
            });
        };
        next = next.bind(undefined, request, response);
        userFilter(request, response, next);
    });

    it("测试传入有效uid的情况。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search?uid=' + tmpUsers[0].id
        });
        var response = httpMocks.createExpressResponse();
        var next = function (_req, _res) {
            testutils.check(done, function () {
                assert.notEqual(_req.user, null);
                _req.user.id.should.equal(tmpUsers[0].id);
            });
        };
        next = next.bind(undefined, request, response);
        userFilter(request, response, next);
    });

    it("测试传入已过期的uid的情况。", function (done) {
        var uid02 = lz.Utils.getUUID("t_");
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search?uid=' + uid02
        });
        var response = httpMocks.createExpressResponse();
        var next = function (_req, _res) {
            testutils.check(done, function () {
                assert.notEqual(_req.query.uid, null);
                assert.notEqual(_req.query.uid, uid02);
            });
        };
        next = next.bind(undefined, request, response);
        userFilter(request, response, next);
    });

    after(function (done) {
        var tasks = [];
        tmpUsers.forEach(function(_user) {
            tasks.push(function(_cb) {
                _user.delete(function() {
                    _cb(null);
                });
            });
        });
        async.series(tasks, function() {
            done();
        });
    });
});